package bb.lanxing.mvp.model;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baidu.mapapi.model.LatLng;

import bb.lanxing.chart.sample.LushuPointDistanceSample;
import bb.lanxing.chart.sample.SportMapRoutePointDisSample;
import bb.lanxing.chart.sample.result.i.ISportMapPointSampleResult;
import bb.lanxing.model.database.IRouteBookPoint;
import bb.lanxing.model.database.RouteBook;
import bb.lanxing.model.database.RouteBookPoint;
import bb.lanxing.model.json.ServerJson;
import bb.lanxing.mvp.model.i.IAltitudeModel;
//import bb.lanxing.network.BiciHttpClient;
import bb.lanxing.network.MyHttpClient;
import bb.lanxing.network.NetSubscribe;
import rx.Observable;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class AltitudeModelImpl implements IAltitudeModel {
    @Override
    public Observable<List<RouteBookPoint>> requestLushuAltitudePoint(long j, int i) {
        return Observable.create(new NetSubscribe(MyHttpClient.requestLushuElevations(j, i)))
                .subscribeOn(Schedulers.io()).flatMap(new Func1<String, Observable<List<RouteBookPoint>>>() {
                    @Override
                    public Observable<List<RouteBookPoint>> call(String str) {
                        ServerJson serverJson = (ServerJson) JSON.parseObject(str, ServerJson.class);
                        if (serverJson.getResult() == 1) {
                            JSONObject jSONObject =
                                    (JSONObject) JSON.parseObject(serverJson.getData(), JSONObject.class);
                            JSONArray jSONArray = jSONObject.getJSONArray("latlngs");
                            JSONArray jSONArray2 = jSONObject.getJSONArray("altitudes");
                            if (jSONArray != null && jSONArray2 != null && jSONArray.size() == jSONArray2.size()) {
                                ArrayList arrayList = new ArrayList(jSONArray.size());
                                for (int i2 = 0; i2 < jSONArray.size(); i2++) {
                                    JSONArray jSONArray3 = jSONArray.getJSONArray(i2);
                                    RouteBookPoint lushuPoint = new RouteBookPoint();
                                    lushuPoint.setLatitude(jSONArray3.getDouble(0));
                                    lushuPoint.setLongitude(jSONArray3.getDouble(1));
                                    lushuPoint.setAltitude(jSONArray2.getDouble(i2));
                                    arrayList.add(lushuPoint);
                                }
                                return Observable.just(arrayList);
                            }
                        }
                        return Observable.error(new IllegalStateException("request route elevation failed !"));
                    }
                });
    }

    @Override
    public Observable<List<RouteBookPoint>> requestPointAltitude(List<LatLng> list) {
        return Observable.create(new NetSubscribe(MyHttpClient.requestElevations(list))).subscribeOn(Schedulers.io())
                .flatMap(new Func1<String, Observable<List<RouteBookPoint>>>() {
                    @Override
                    public Observable<List<RouteBookPoint>> call(String str) {
                        JSONObject parseObject = JSON.parseObject(str);
                        if ("OK".equalsIgnoreCase(parseObject.getString("status"))) {
                            JSONArray jSONArray = parseObject.getJSONArray("results");
                            ArrayList arrayList = new ArrayList(jSONArray.size());
                            for (int i = 0; i < jSONArray.size(); i++) {
                                JSONObject jSONObject = (JSONObject) jSONArray.get(i);
                                double doubleValue = jSONObject.getDoubleValue("elevation");
                                JSONObject jSONObject2 = jSONObject.getJSONObject("location");
                                double doubleValue2 = jSONObject2.getDoubleValue("lat");
                                double doubleValue3 = jSONObject2.getDoubleValue("lng");
                                RouteBookPoint lushuPoint = new RouteBookPoint();
                                lushuPoint.setAltitude(doubleValue);
                                lushuPoint.setLatitude(doubleValue2);
                                lushuPoint.setLongitude(doubleValue3);
                                arrayList.add(lushuPoint);
                            }
                            return Observable.just(arrayList);
                        }
                        return Observable.error(new IllegalStateException("request points elevation failed !"));
                    }
                });
    }

    @Override
    public Observable<List<LatLng>> getSampleRoutePoint(long id, final double d) {
        return Observable.just(id).subscribeOn(Schedulers.io())
                .flatMap(l -> Observable.just(RouteBookPoint.getByLushuId(l))).subscribeOn(Schedulers.computation())
                .flatMap(list -> {
                    List<IRouteBookPoint> sampleDataSource =
                            new LushuPointDistanceSample().sample(d, list, 200).getSampleDataSource();
                    ArrayList<LatLng> arrayList = new ArrayList<>(sampleDataSource.size());
                    for (IRouteBookPoint iRouteBookPoint : sampleDataSource) {
                        arrayList.add(new LatLng(iRouteBookPoint.getLatitude(), iRouteBookPoint.getLongitude()));
                    }
                    return Observable.just(arrayList);
                });
    }

    @Override
    public Observable<ISportMapPointSampleResult> getAltitudeSamplePointData(final List<RouteBookPoint> list,
            final double d) {
        return Observable.just(d).subscribeOn(Schedulers.io()).flatMap(d2 -> {
            SportMapRoutePointDisSample sportMapRoutePointDisSample = new SportMapRoutePointDisSample();
            double d3 = d;
            List<? extends IRouteBookPoint> list2 = list;
            return Observable
                    .just((ISportMapPointSampleResult) sportMapRoutePointDisSample.sample(d3, list2, list2.size()));
        });
    }

    @Override
    public String saveAltitudePoints(long j, List<RouteBookPoint> list) {
        RouteBook byId = RouteBook.getById(j);
        if (byId == null) {
            return null;
        }
        JSONArray jSONArray = new JSONArray(list.size());
        for (int i = 0; i < list.size(); i++) {
            RouteBookPoint lushuPoint = list.get(i);
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("lat", (Object) lushuPoint.getLatitude());
            jSONObject.put("lng", (Object) lushuPoint.getLongitude());
            JSONObject jSONObject2 = new JSONObject();
            jSONObject2.put("elevation", (Object) lushuPoint.getAltitude());
            jSONObject2.put("location", (Object) jSONObject);
            jSONArray.add(jSONObject2);
        }
        String jSONArray2 = jSONArray.toString();
        byId.setAltitudeString(jSONArray2);
        byId.save();
        return jSONArray2;
    }
}